<template>
	<view class="">
		<view class="bg">
			<image
				src="https://aihelp.oss-cn-hangzhou.aliyuncs.com/public/base/2023/10/16/e8188a95642744adaadce4788e399424.png">
			</image>
		</view>
		<view class="font14 flex justConBetween compareMain" :style="{'height':scrollHeight+'px'}">
			<scroll-view scroll-y="true" class="scrollPro">
				<view
					:class="(chooseLib&&chooseLib.id==item.id)?'proItem activePro bold flex alignCenter':'proItem flex alignCenter'"
					v-for="(item,index) in compareLib" :key="index" @click.stop="chooseLibClick(item,index)">
					<view class="hiddenTwoline">{{item.name}}</view>
					<image src="@/static/image/toolbox/edit.png" mode="widthFix" @click.stop="editLibName(item)"></image>
				</view>
			</scroll-view>
			<view class="schoolBox">
				<view class="flex alignCenter justConAround searchBox font4">
					<view class="noWidthBtn" @click="clickSearch('school')">按大学</view>
					<view class="noWidthBtn" @click="clickSearch('major')">按专业</view>
					<view class="noWidthBtn" @click="clickSearch('lib')">备选库</view>
				</view>
				<view class="flex alignCenter bold comNum">
					<text class="colorMain font18">{{schoolArr.length}}</text>
					<text class="font14">/3</text>
				</view>
				<view class="schoolModule">
					<view class="schoolOne" v-for="(item,index) in schoolArr" :key="index">
						<view class="flex alignCenter" v-if="item.cover">
							<image class="logo"
								:src="item.cover.logo || 'https://image.alhelp.net/uploads/20231104/4d491857ac50d21ebbcb25ff5bc254f8.png'"
								mode="widthFix"></image>
							<view class="schoolInfo">
								<view class="flex alignCenter justConBetween ">
									<view class="hiddenOneline font16 bold">
										{{item.cover.schoolName || ''}}-{{item.cover.collegeName || ''}}
									</view>
									<view class="font14 schoolType" v-if="item.cover.studyType">{{item.cover.studyType || ''}}</view>
								</view>
								<view class="hiddenOneline font14">{{item.cover.majorCode || ''}} {{item.cover.majorName || ''}}</view>
							</view>
						</view>
						<view class="schoolOper">
							<uni-icons type="trash" color="#aaaaaa" size="20" @click="delSchool(index,item)"></uni-icons>
						</view>
					</view>
					<uni-nodata v-if="schoolArr.length==0"></uni-nodata>
				</view>
			</view>
		</view>
		<view class="fixedBottomBtn" v-if="(!showPicker && !showLib)" @click="toCompare"><i
				class="iconfont icon-fabu"></i>开始对比</view>
		<!-- 按大学 -->
		<view class="pickerLayer" v-if="showPicker">
			<view class="layerMain">
				<view class="layerTitle">
					{{this.searchType=='school'?'按大学选择':'按专业选择'}}
					<uni-icons class="layerClose" type="closeempty" size="20" @click="closeClick"></uni-icons>
				</view>
				<scroll-view scroll-x="true">
					<view class="flex alignCenter font14 chooseBox">
						<view v-if="layerActiveChoose>=index"
							:class="layerActiveChoose==index?'commonChoose activeLayerStyle hiddenOneline':'commonChoose hiddenOneline'"
							v-for="(item,index) in chooseArr" :key="index" @click="clickLayerTitle(index)">
							{{item.chooseData?item.chooseData.scrollTitle:'请选择'}}
						</view>
					</view>
				</scroll-view>
				<!-- 请求展示的·数据 -->
				<view class="dataBox">
					<scroll-view scroll-y="true" class="font14 dataScroll">
						<view
							:class="chooseArr[layerActiveChoose].chooseData.id==item.id?'scrollItem colorMain hiddenOneline':'scrollItem hiddenOneline'"
							v-for="(item,index) in scrollData" :key="index" @click="clickScrollItem(item)">
							{{item.scrollTitle}}
							<uni-icons v-if="chooseArr[layerActiveChoose].chooseData.id==item.id" class="activeIcon"
								type="checkmarkempty" size="16"></uni-icons>
						</view>
					</scroll-view>
				</view>
			</view>
		</view>
		<!-- 备选库 -->
		<view class="pickerLayer" v-if="showLib">
			<view class="layerMain">
				<view class="layerTitle">
					备选库
					<uni-icons class="layerClose" type="closeempty" size="20" @click="closeClick"></uni-icons>
				</view>
				<!-- 请求展示的·数据 -->
				<view class="dataBox">
					<scroll-view scroll-y="true" class="font14 dataScroll">
						<view class="schoolOne" v-for="(item,index) in moreLibrary" :key="index" @click="clickLib(item)">
							<view class="flex alignCenter justConBetween">
								<view class="flex alignCenter">
									<image class="logo"
										:src="item.logo || 'https://image.alhelp.net/uploads/20231104/4d491857ac50d21ebbcb25ff5bc254f8.png'"
										mode="widthFix"></image>
									<view class="schoolInfo">
										<view class="flex alignCenter justConBetween ">
											<view class="hiddenOneline font16 bold">{{item.schoolName}}-{{item.collegeName}}</view>
											<view class="font14 schoolType" v-if="item.studyType">{{item.studyType}}</view>
										</view>
										<view class="hiddenOneline font14">{{item.majorCode}} {{item.majorName}}</view>
									</view>
								</view>
								<view class="schoolOper">
									<uni-icons type="trash" color="#aaaaaa" size="16" @click="delLibSchool(item,index)"></uni-icons>
								</view>
							</view>
							<image class="chooseIcon" v-if="item.choose" src="@/static/image/icons/cr.png" mode="widthFix"></image>
						</view>
						<uni-nodata v-if="moreLibrary.length==0" :iconStyle="{'margin':'100rpx 0 0'}"></uni-nodata>
						<view v-if="moreLibrary.length>0" class="fixedBottomBtn" @click="sureLib">确定</view>
					</scroll-view>
				</view>
			</view>
		</view>
		<!-- 修改库名 -->
		<u-modal v-model="inputDialog" show-cancel-button	 title="修改库名" @cancel="dialogInputClose" @confirm="dialogInputConfirm">
			<view class="slot-content u-p-40">
				<u-input border v-model="keywrod" placeholder="最多10个字符"></u-input>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {
		getProvince,
		getSchoolByPro,
		getCollegeBySch,
		getMajorByColl,
		getSubject,
		getMajorBySubject,
		getSchoolBySubject
	} from "@/api/index.js"
	import {
		addSchoolToCompare,
		getContrastList,
		getContrastSchool,
		listCover,
		getContrastLib,
		editLibraryName,
		delLibrary,
		addDetail,
		contrastDetailDel
	} from "@/api/toolbox.js"
	export default {
		data() {
			return {
				keywrod:"",
				inputDialog: false,
				schoolArr: [],
				chooseLib: '',
				compareLib: [],
				scrollHeight: 0,
				province: '',
				school: '',
				major: '',
				scrollData: [],
				layerActiveChoose: 0,
				chooseArr: [{
					chooseData: ''
				}, {
					chooseData: ''
				}, {
					chooseData: ''
				}, {
					chooseData: ''
				}],
				showPicker: false,
				showLib: false,
				searchType: '',
				isClose: false, //是否在弹出框选择了x
				moreLibrary: [],
				libOldName: '',
				editShow: false, //主要用来解决uni-popup-dialog 对话框在小程序ios上未显示也会自动聚焦input导致键盘调起的bug
			}
		},
		watch: {
			showPicker(n, o) {
				if (!n && !this.isClose) {
					this.addToCompare()
				}
			}
		},
		onLoad() {
			this.getLeftTree()
		},
		onShow() {

		},
		onReady() {
			this.$nextTick(() => {
				const query = uni.createSelectorQuery().in(this);
				query.select('.bg').boundingClientRect(data => {
					this.scrollHeight = uni.getStorageSync('systemInfo').screenHeight - data.height - 44
				}).exec();
			})
		},
		methods: {
			// 获取对比库左侧的列表
			getLeftTree() {
				getContrastList().then((res) => {
					if (res.code == 100000) {
						this.compareLib = res.data
						this.chooseLib = this.compareLib[0]
						this.getSchoolByLeftTree()
					}
				})
			},
			// 根据选择的左侧的对比库展示学校
			getSchoolByLeftTree() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				listCover({
					id: this.chooseLib.id
				}).then((res) => {
					uni.hideLoading()
					if (res.code == 100000) {
						this.schoolArr = res.data
					}
				}).catch((err) => {
					uni.hideLoading()
				})
			},
			// 获取对比备选库
			getMoreLib() {
				getContrastLib().then((res) => {
					if (res.code == 100000) {
						res.data.forEach((item) => {
							item.choose = false
						})
						this.moreLibrary = res.data
					}
				})
			},
			// 省
			getProList() {
				getProvince().then((res) => {
					if (res.code == 100000) {
						res.data.forEach((item) => {
							item.scrollTitle = item.fullName
						})
						this.scrollData = res.data
					}
				})
			},
			// 学校
			getSchoolList() {
				let param = {
					areaId: this.chooseArr[0].chooseData.id,
					pageNum: 1,
					pageSize: 999999,
					reasonable: true
				}
				getSchoolByPro(param).then((res) => {
					if (res.code == 100000) {
						res.data.rows.forEach((item) => {
							item.scrollTitle = item.title
						})
						this.scrollData = res.data.rows
					}
				})
			},
			// 根据学校获取学院
			getColleage() {
				let param = {
					schoolCode: this.chooseArr[1].chooseData.schoolCode,
					year: new Date().getFullYear()
				}
				getCollegeBySch(param).then((res) => {
					if (res.code == 100000) {
						res.data.forEach((item) => {
							item.scrollTitle = `(${item.schoolCode})${item.title}`
						})
						this.scrollData = res.data
					}
				})
			},
			// 专业
			getMajorList() {
				let param = {
					collegeCode: this.chooseArr[2].chooseData.collegeCode,
					schoolCode: this.chooseArr[1].chooseData.schoolCode,
					year: new Date().getFullYear()
				}
				getMajorByColl(param).then((res) => {
					if (res.code == 100000) {
						// 因为有些专业选择到这儿就没有数据了，就直接结束
						if (res.data.length == 0) {
							this.showPicker = false
							return
						}
						res.data.forEach((item) => {
							item.scrollTitle = `(${item.majorCode})${item.title}`
						})
						this.scrollData = res.data
					}
				})
			},
			// 获取学科门类
			getSubjectList(type) {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				let param = {
					pageNum: 1,
					pageSize: 99999,
					type: type,
					year: new Date().getFullYear(),
				}
				if (type > 0) {
					param.subjectCode = this.chooseArr[0].chooseData.subjectCode
				}
				getSubject(param).then((res) => {
					uni.hideLoading()
					if (res.code == 100000) {
						res.data.rows.forEach((item) => {
							item.scrollTitle = `(${item.subjectCode})${item.title}`
						})
						this.scrollData = res.data.rows
					}
				}).catch((err) => {
					uni.hideLoading()
				})
			},
			// 根据学科门类获取专业
			getMajorFromSubject() {
				let param = {
					year: new Date().getFullYear(),
					subjectOneCode: this.chooseArr[1].chooseData.subjectCode
				}
				getMajorBySubject(param).then((res) => {
					if (res.code == 100000) {
						res.data.forEach((item) => {
							item.scrollTitle = `(${item.majorCode})${item.title}`
						})
						this.scrollData = res.data
						if (res.data.length == 0) {
							this.showPicker = false
						}
					}
				})
			},
			// 根据选择的学科专业获取学校及专业
			getSchoolFromSubject() {
				getSchoolBySubject({
					majorCode: this.chooseArr[2].chooseData.majorCode
				}).then((res) => {
					if (res.code == 100000) {
						res.data.forEach((item) => {
							item.scrollTitle = `${item.schoolName}/${item.collegeName}`
						})
						this.scrollData = res.data
					}
				})
			},
			// 左侧选择库事件
			chooseLibClick(item) {
				this.isClose = false
				this.chooseLib = item
				this.schoolArr = []
				this.getSchoolByLeftTree()
			},
			// 左侧库的长按事件
			editLibName(item) {
				this.editShow = true
				this.libOldName = item;
				this.keywrod = JSON.parse(JSON.stringify(this.libOldName.name));
				this.inputDialog = true;
				// this.$refs.inputDialog.open()
			},
			// 修改名字的确定事件
			dialogInputConfirm() {
				let val = this.keywrod;
				// this.value = val
				if (!val || val.length > 10) {
					uni.showToast({
						title: '库名需在10个字符及以内',
						icon: 'none'
					})
					return
				}
				let param = Object.assign(this.libOldName, {
					name: val
				})
				editLibraryName([param]).then((res) => {
					if (res.code == 100000) {
						uni.showToast({
							title: '修改成功',
							icon: 'none'
						})
						this.libOldName.name = val
						// 关闭窗口后，恢复默认内容
						this.editShow = false
						this.inputDialog = false;
						// this.$refs.inputDialog.close()
					}
				})
			},
			dialogInputClose() {
				this.editShow = false
				this.inputDialog = false;
				// this.$refs.inputDialog.close()
			},
			// 点击搜索条件
			clickSearch(type) {
				if (this.schoolArr.length >= 3 && type != 'lib') {
					uni.showToast({
						title: '同时最多对比3所高校',
						icon: 'none'
					})
					return
				}
				this.isClose = false
				this.searchType = type
				this.layerActiveChoose = 0
				Object.assign(this.$data.chooseArr, this.$options.data().chooseArr)
				if (type == 'school') {
					this.getProList()
					this.showPicker = true
				} else if (type == 'major') {
					this.getSubjectList(0)
					this.showPicker = true
				} else if (type == 'lib') {
					this.getMoreLib()
					this.showLib = true
				}
			},
			// 蒙层的头部模块的点击事件
			clickLayerTitle(idx) {
				this.layerActiveChoose = idx
				this.dealSearchDiffToApi('scrollTitle')
			},
			// scroll的选择点击事件
			clickScrollItem(item) {
				this.chooseArr[this.layerActiveChoose].chooseData = item
				this.chooseArr.forEach((val, index) => {
					if (index > this.layerActiveChoose) {
						val.chooseData = ''
					}
				})
				this.dealSearchDiffToApi('scrollItem')
				if (this.layerActiveChoose >= 3) {
					this.layerActiveChoose = 3
					this.showPicker = false
					return
				}
				this.layerActiveChoose++
			},
			// 根据搜索条件选择调用相应的api的处理方法
			dealSearchDiffToApi(clickModule) {
				if (clickModule == 'scrollItem') {
					if (this.searchType == 'school') {
						if (this.layerActiveChoose == 0) {
							this.getSchoolList()
						} else if (this.layerActiveChoose == 1) {
							this.getColleage()
						} else if (this.layerActiveChoose == 2) {
							this.getMajorList()
						}
					} else if (this.searchType == 'major') {
						if (this.layerActiveChoose == 0) {
							this.getSubjectList(1)
						} else if (this.layerActiveChoose == 1) {
							this.getMajorFromSubject()
						} else if (this.layerActiveChoose == 2) {
							this.getSchoolFromSubject()
						}
					}
					return
				} else if (clickModule == 'scrollTitle') {
					if (this.searchType == 'school') {
						if (this.layerActiveChoose == 0) {
							this.getProList()
						} else if (this.layerActiveChoose == 1) {
							this.getSchoolList()
						} else if (this.layerActiveChoose == 2) {
							this.getColleage()
						} else if (this.layerActiveChoose == 3) {
							this.getMajorList()
						}
					} else if (this.searchType == 'major') {
						if (this.layerActiveChoose == 0) {
							this.getSubjectList(0)
						} else if (this.layerActiveChoose == 1) {
							this.getSubjectList(1)
						} else if (this.layerActiveChoose == 2) {
							this.getMajorFromSubject()
						} else if (this.layerActiveChoose == 3) {
							this.getSchoolFromSubject()
						}
					}
				}
			},
			// 加入对比库
			addToCompare() {
				uni.showLoading({
					title: '加入中',
					mask: true
				})
				let param = ''
				if (this.searchType == 'school') {
					param = {
						"schoolCode": this.chooseArr[1].chooseData.schoolCode,
						"year": new Date().getFullYear(),
						"collegeCode": this.chooseArr[2].chooseData.collegeCode,
						"majorCode": this.chooseArr[3].chooseData.majorCode,
						"comparisonLibraryId": this.chooseLib.id,
					}
				} else if (this.searchType == 'major') {
					param = {
						"schoolCode": this.chooseArr[3].chooseData.schoolCode,
						"year": new Date().getFullYear(),
						"collegeCode": this.chooseArr[3].chooseData.collegeCode,
						"majorCode": this.chooseArr[2].chooseData.majorCode,
						"comparisonLibraryId": this.chooseLib.id,
					}
				}
				addDetail(param).then((res) => {
					uni.hideLoading()
					if (res.code == 100000) {
						this.schoolArr.push(res.data)
					}
				}).catch((err) => {
					uni.hideLoading()
				})

				// addSchoolToCompare(param).then((res) => {
				// 	console.log("加入对比", res)
				// 	uni.hideLoading()
				// 	if (res.code == 100000) {
				// 		this.schoolArr.push(res.data)
				// 	}
				// }).catch((err) => {
				// 	uni.hideLoading()
				// })
			},
			// 删除对比学校
			delSchool(idx, row) {
				if (row.cover.detailId) {
					uni.showModal({
						title: '提示',
						content: '确认删除该对比？',
						showCancel: true,
						cancelText: '取消',
						confirmText: '删除',
						success: modal => {
							if (modal.confirm) {
								uni.showLoading({
									mask: true
								});
								contrastDetailDel(row.cover.detailId).then(res => {
									if (res.code == 100000) {
										this.schoolArr.splice(idx, 1)
									}
								}).finally(() => {
									uni.hideLoading();
								})
							}
						},
					});
				} else {
					uni.showToast({
						title: '对比库ID有误！',
						icon: 'none'
					});
				}
			},
			// 删除对比库中的学校
			delLibSchool(item, idx) {
				uni.showModal({
					title: '提示',
					content: '删除后该数据将不再存在备选库中，是否确定删除？',
					success: (res) => {
						if (res.confirm) {
							delLibrary([item.id]).then((res) => {
								if (res.code == 100000) {
									this.moreLibrary.splice(idx, 1)
								}
							})
						} else if (res.cancel) {}
					}
				});
			},
			// 关闭选择弹框
			closeClick() {
				this.isClose = true
				this.showLib = false
				this.showPicker = false
			},
			// 选择对比备选库
			clickLib(item) {
				item.choose = !item.choose
			},
			// 备选库确定事件
			sureLib() {
				let chooseData = this.moreLibrary.filter((item) => item.choose)
				if (chooseData.length > 3) {
					uni.showToast({
						title: '同时最多对比3所高校',
						icon: 'none'
					})
					return
				}
				if (chooseData.length == 0) {
					uni.showToast({
						title: '请先选择要对比的学校，最多3所',
						icon: 'none'
					})
					return
				}
				// console.error("数据===",chooseData)
				let queryArr = []
				chooseData.forEach((item) => {
					let itemObj = {
						"schoolCode": item.schoolCode,
						"year": new Date().getFullYear(),
						"collegeCode": item.collegeCode,
						"majorCode": item.majorCode,
					}
					queryArr.push(itemObj)
				})
				uni.navigateTo({
					url: '/pages/toolbox/comparisonLibrary/compareDetail/compareDetail?query=' + JSON.stringify(queryArr)
				})
			},
			// 去对比
			toCompare() {
				if (this.schoolArr.length == 0) {
					uni.showToast({
						title: '请先选择要对比的学校',
						icon: 'none'
					})
					return
				} else if (this.schoolArr.length > 3) {
					uni.showToast({
						title: '同时最多对比3所高校,请先删除部分数据后再进行对比',
						icon: 'none'
					})
					return
				}
				let queryData = []
				this.schoolArr.forEach((item) => {
					let itemObj = {
						"schoolCode": item.cover.schoolCode,
						"year": new Date().getFullYear(),
						"collegeCode": item.cover.collegeCode,
						"majorCode": item.cover.majorCode,
					}
					queryData.push(itemObj)
				})
				uni.navigateTo({
					url: '/pages/toolbox/comparisonLibrary/compareDetail/compareDetail?query=' + JSON.stringify(queryData)
				})
			}
		}
	}
</script>
<style lang="scss">
	page {
		background: $pageBgColor;
	}
</style>
<style scoped lang="scss">
	scroll-view ::-webkit-scrollbar {
		width: 0;
		height: 0;
		background-color: transparent;
	}

	.bg {
		height: 216rpx;

		image {
			width: 100%;
			height: 100%;
			display: block;
			border-radius: 0 0 80rpx 80rpx;
		}
	}

	.scrollPro {
		height: 100%;
		overflow-y: scroll;
		width: 180rpx;
		flex-shrink: 0;
	}

	.compareMain {
		// height:calc(100% - 216rpx);//还有tabbar
		overflow: hidden;
	}

	.proItem {
		background: #fff;
		padding: 30rpx 10rpx;
		box-sizing: border-box;
		display: flex;

		image {
			width: 40rpx;
			height: 40rpx;
			flex-shrink: 0;
			margin-left: 10rpx;
		}
	}

	.activePro {
		background: $pageBgColor;
	}

	.schoolBox {
		height: 100%;
		overflow-y: scroll;
		flex: 1;
		flex-shrink: 0;
		padding: 30rpx 30rpx 100rpx;
		box-sizing: border-box;

		.comNum {
			margin: 30rpx 0;
			padding: 0 0 0 30rpx;
			box-sizing: border-box;
		}

		.schoolModule {
			.schoolInfo {
				flex: 1;

				.schoolType {
					flex-shrink: 0;
				}
			}
		}
	}

	.schoolOne {
		position: relative;
		background: #fff;
		border-radius: 20rpx;
		padding: 20rpx;
		box-sizing: border-box;
		margin-bottom: 30rpx;

		.chooseIcon {
			width: 50rpx;
			height: 50rpx;
			position: absolute;
			right: 0;
			top: 0;
			border-radius: 0 20rpx 0 0;
		}

		.logo {
			width: 70rpx;
			height: 70rpx;
			flex-shrink: 0;
			margin: 0 20rpx 0 0;
			border-radius: 50%;
		}

		.schoolOper {
			text-align: right;
		}
	}

	.pickerLayer {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background: rgba(0, 0, 0, .15);

		.layerMain {
			position: absolute;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 1000rpx;
			padding: 30rpx 0;
			box-sizing: border-box;
			border-radius: 40rpx 40rpx 0 0;
			background: #fff;
		}

		.layerTitle {
			text-align: center;
			position: relative;

			.layerClose {
				position: absolute;
				right: 30rpx;
				bottom: 0;
			}
		}

		scroll-view {
			margin: 30rpx 0 0;
		}

		.chooseBox {
			// margin: 30rpx 0 0;
			padding: 0 30rpx;
			box-sizing: border-box;
			border-bottom: 1px solid $bordercolor;
			height: 80rpx;
			line-height: 80rpx;

			.commonChoose {
				padding: 0 40rpx;
				flex-shrink: 0;
			}

			.activeLayerStyle {
				border-bottom: 2px solid $colorMain;
			}
		}

		.dataBox {
			padding: 30rpx;
			box-sizing: border-box;

			.schoolOne {
				border: 1px solid $bordercolor;
			}

			.dataScroll {
				height: 800rpx;
				overflow-y: scroll;
			}

			.scrollItem {
				padding: 0 30rpx;
				height: 100rpx;
				line-height: 100rpx;
				box-sizing: border-box;
				position: relative;

			}

			.activeIcon {
				color: $colorMain !important;
				position: absolute;
				right: 30rpx;
				bottom: 0;
			}
		}
	}
</style>